def checker():
    with open("candidates_list","r") as f:
        return [y for y in [int(x) for x in [z for z in f.read().split("\n") if len(z) > 0]] if y != 5]

import os
import multiprocessing
import time

def run_against(target_dir):
    print("cloning missing target:",target_dir)
    cmd = "git clone https://github.com/james6deutschland/{}".format(target_dir)
    os.system(cmd)
def run_regular(target_dir):
    print("initializing daemon script for:",target_dir)
    script_name ="{}.sh".format(target_dir)
    if not os.path.exists(script_name):
        with open(script_name,"w+") as f:
            f.write("cd {} && bash autocommit.sh\n".format(target_dir))
    cmd = script_name
    os.system("bash {}".format(script_name))
def spare_run_regular(target_dir):
    multiprocessing.Process(target=run_regular,args=(target_dir,)).start()
def format_candidates(num_list):
    return ["littlerepo{}".format(x) for x in num_list]
def check_missing(_list):
    dirlist = os.listdir(".")
    return [x for x in _list if x not in dirlist]

init = True
while True:
    new_list = checker()
    targets = format_candidates(new_list)
    missing = check_missing(targets)
    online = [x for x in targets if x not in missing]
    print("targets:",targets)
    print("missing:",missing)
    print("online:",online)
    if init:
        for x in online:
            spare_run_regular(x)
    init = False
    for x in missing:
        run_against(x)
        spare_run_regular(x)
    time.sleep(5)
